GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Branch decomplexify (3ec600)
by Oliver
02:19
created

floatify.js ➔ ... ➔ parse   F

Complexity

Conditions 16
Paths 13

Size

Total Lines 99
Code Lines 50

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 40
CRAP Score 16

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 16
eloc 50
c 3
b 0
f 0
nc 13
nop 1
dl 0
loc 99
ccs 40
cts 40
cp 1
crap 16
rs 2.4

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Complexity

Complex classes like floatify.js ➔ ... ➔ parse often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
'use strict';
2
3 1
var floatify = function floatify(str) {
4 279
  var toFloatFormat = function toFloatFormat(str, ts, ds) {
5 229
    var string = str;
6 229
    var decimalSeparator = ds || '';
7
8 229
    string = string.split(ts || '').join('');
9 229
    if (decimalSeparator !== '') {
10 195
      string = string.split(decimalSeparator).join('.');
11
    }
12
13 229
    return parseFloat(string);
14
  };
15
16 279
  var parseParts = function parseParts(str, ele, count) {
17 165
    var string = str;
18 165
    var element = ele;
19 165
    var parts = string.split(element);
20
21
    function parseMidPart() {
22 25
      if (current.length !== 3) {
23 16
        return Number.NaN;
24
      }
25
26 9
      if (left.length > 3) {
27 2
        return Number.NaN;
28
      }
29
30
      // no decision, continue
31 7
      return null;
32
    }
33
34
    function parseEndPart() {
35 147
      if ((leftVal === 0 || isNaN(leftVal) || left.length > 3)) {
36 94
        return toFloatFormat(string, '', element);
37
      }
38
39 53
      if (current.length === 3) {
40 11
        return toFloatFormat(string, element, '');
41
      }
42
43 42
      if (count === 1) {
44 38
        return toFloatFormat(string, '', element);
45
      }
46
47 4
      return Number.NaN;
48
    }
49
50 165
    for (var i = 1; i < parts.length; i++) {
51 172
      var current = parts[i];
52 172
      var left = parts[i - 1];
53 172
      var leftVal = parseInt(left, 10);
54 172
      var isLast = parts.length - 1 === i;
55
      var parseResult;
56
57 172
      if (!isLast) {
58 25
        parseResult = parseMidPart();
59
      } else {
60 147
        parseResult = parseEndPart();
61
      }
62
63 172
      if (parseResult !== null) {
64 165
        break;
65
      }
66
    }
67 165
    return parseResult || Number.NaN;
68
  };
69
70 279
  var parse = function parse(str) {
71 279
    var string = str;
72
    var spacePos;
73
    var spaceSplit;
74
    var spaceCount;
75
    var dotPos;
76
    var commaPos;
77
    var lDotPos;
78
    var lCommaPos;
79
    var dotCount;
80
    var commaCount;
81
82
    function parseMixedSeparators() {
83
      // format is using dot and comma
84
85
      // last dot position
86 68
      lDotPos = string.lastIndexOf('.');
87
      // last comma position
88 68
      lCommaPos = string.lastIndexOf(',');
89
90
      // order of 1st dot -> comma must be same as last dot -> comma
91
      // 123.123.123,123 -> ok 123.123,123.123 -> not ok
92 68
      if (Math.sign(dotPos - commaPos) !== Math.sign(lDotPos - lCommaPos)) {
93 3
        return Number.NaN;
94
      }
95
96
      // check positions to guess the thousands separator
97 65
      if (dotPos > commaPos) {
98 57
        if (dotCount > 1) {
99 1
          return Number.NaN;
100
        }
101
        // best guess: . is thousands separator and , is decimal point
102 56
        return toFloatFormat(string, ',', '.');
103
      }
104
105 8
      if (commaCount > 1) {
106 1
        return Number.NaN;
107
      }
108
      // best guess: , is thousands separator and . is decimal point
109 7
      return toFloatFormat(string, '.', ',');
110
    }
111
112 279
    string = string.trim();
113
114
    // 1st dot position
115 279
    dotPos = string.indexOf('.');
116
    // 1st comma position
117 279
    commaPos = string.indexOf(',');
118
    // 1st space position
119 279
    spacePos = string.indexOf(' ');
120
121 279
    if (dotPos + commaPos + spacePos === -3) {
122
      // life is good, no separators
123 18
      return toFloatFormat(string);
124
    }
125
126 261
    spaceSplit = string.split(' ');
127 261
    spaceCount = spaceSplit.length - 1;
128 261
    dotCount = string.split('.').length - 1;
129 261
    commaCount = string.split(',').length - 1;
130
131
    // only combination of 2 separators allowed
132 261
    if (dotCount > 0 && commaCount > 0 && spaceCount > 0) {
133 1
      return Number.NaN;
134
    }
135
136
    // if there is any separator (space, comma, dot) found more than once,
137
    // all other must not be found more than once
138 260
    if (dotCount > 1 && (commaCount > 1 || spaceCount > 1)) {
139 4
      return Number.NaN;
140
    }
141
142 256
    if (commaCount > 1 && spaceCount > 1) {
143 1
      return Number.NaN;
144
    }
145
146 255
    if (spaceCount > 0) {
147 72
      if (!string.match(/^(\d{1,3})?(\s\d{3})*([,\.]\d+)?$/)) {
148 17
        return Number.NaN;
149
      }
150 55
      string = spaceSplit.join('');
151
    }
152
153 238
    if (dotPos !== -1 && commaPos !== -1) {
154 68
      return parseMixedSeparators();
155
    }
156
157 170
    if (dotPos !== -1) {
158
      // only dot(s) in format
159 85
      return parseParts(string, '.', dotCount);
160
    }
161
162 85
    if (commaPos !== -1) {
163
      // only comma(s) in format
164 80
      return parseParts(string, ',', commaCount);
165
    }
166
167 5
    return toFloatFormat(string);
168
  };
169
170 279
  return parse(str);
171
};
172
173 2
if (typeof exports !== 'undefined') {
174 4
  if (typeof module !== 'undefined' && module.exports) {
175 1
    exports = module.exports = floatify;
176
  }
177
}
178